<?php

namespace app\home\model;
use app\home\service\LimitAmount;
use Think\Db;
use think\Model;

class UserOrder extends Model
{
        protected $tableName = 'user_order_info';
    
        public function user_search_order_new($orderid,$total_price){
            $Dbname = Db::connect('db_config2');
            $field = '
                 userid,
                 goods_id,
                 amount,
                 status,
                 channel_id,
                 extra
                 ';
            $order_info =$Dbname->table('tradelog')->field($field)->where('id',$orderid)->find();
            if(empty($order_info)){
                mylog($orderid.' 订单不存在');
                return false;
            }

            if($order_info['amount']!=$total_price){
                mylog($orderid.' 订单金额不一致'.' 传参:'.$total_price.' 实际:'.$order_info['amount']);
                return false;
            }

            return $order_info;
        }
        
     /*old支付*/
     function  create_order_old($userid,$channel_id,$GoodsId,$tradetype=1,$extra){
         $Dbname = Db::connect('db_config2');
         $OrderId =get_microtimes();
         
         // 限购计费点 一个玩家只能买一次
         if((new Tradelogold())->Check_Forever_Limited_Purchase($userid,$GoodsId)){
             puterror("你已经购买过这个礼包啦，不要太贪心哦！");
         }
         
         /*验证用户合法性*/
         checkout_user_validity($userid);
         checkout_order_validity($userid,date('Y-m-d H:i:s',time()));

         // 充值限额处理
         $limitAmount = new LimitAmount($userid);
         $isCanRecharge = $limitAmount->checkIsCanRecharge();
         if($isCanRecharge != 1){
             puterror(['msg'=>$isCanRecharge]);
         }

         $arr = $Dbname->table('accounts_info')->where('UserID', $userid)->field('SpreaderID,RegisterDate,LoginServer')->find();
         $good_configure_info = getServerGoods($arr['LoginServer'],$GoodsId);
         $data = [
             'id' => $OrderId,
             'userid' => $userid,
             'goods_id' =>$GoodsId,
             'amount' =>$good_configure_info['Price'],
             'status' => 0, //默认是0,表示未付款
             'channel_id' => $channel_id,
             'SpreaderID' => $arr['SpreaderID'],
             'tradetype'=>$tradetype,
             'extra'=>$extra,
             'createdat' => date('Y-m-d H:i:s',time()),
             'registerat'=>$arr['RegisterDate'],
             'loginIp'=>get_client_ip()
         ];
         $res = $Dbname->table('tradelog')->insert($data);
         if($res){
            $return['status'] = '200';
            $return['msg'] = '成功';
            $return['orderid'] = $OrderId;
            $return['price'] = $good_configure_info['Price'];
            $return['subject'] = $good_configure_info['Title'];
            $return['registerat'] = $data['registerat'];
            return  json_encode($return);
         }else{
            puterror('订单生成失败');
         }
    }


}